/**
 * Simple HTML tag renderer.
 *
 * @author Andrey Anisimov <hercules@inbox.ru>
 */

class Tag {
	
	/**
	 * Creates new tag object.
	 *
	 * @param name (string) tag name
	 * @param attrs (object) [optional] tag attributes
	 * @param content (string) [optional] inner content
	 */
	this.Tag = function(name, attrs, content) {
		this.name = name;
		this.attrs = attrs || {};
		this.content = content || '';
	}
	
	/**
	 * Sets the tag name.
	 *
	 * @param name (string) new name
	 */
	this.setName = function(name) {
		this.name = name;
	}
	
	/**
	 * Returns tag name.
	 *
	 * @return string
	 */
	this.getName = function() {
		return this.name;
	}
	
	/**
	 * Sets the tag content.
	 *
	 * @param content (string) new content
	 */
	this.setContent = function(content) {
		this.content = content;
	}
	
	/**
	 * Returns tag content.
	 *
	 * @return string
	 */
	this.getContent = function() {
		return this.content;
	}

	/**
	 * Adds new part to the current content.
	 *
	 * @param part (string) new part.
	 */
	this.addContent = function(part) {
		this.content = this.content.toString() + part;
	}
	
	/**
	 * Sets the tag attribute.
	 *
	 * @param name (string) attribute name
	 * @param value (string) attribute value
	 */
	this.setAttribute = function(name, value) {
		this.attrs[name] = value;
	}
	
	/**
	 * Returns tag attribute value.
	 *
	 * @param name (string) attribute name
	 * @return string
	 */
	this.getAttribute = function(name) {
		return this.attrs[name];
	}

	/**
	 * Checks whether the tag has specified attribute.
	 *
	 * @param name (string) attribute name
	 * @return bool
	 */
	this.hasAttribute = function(name) {
		return (this.attrs[name]);
	}
	
	/**
	 * Removes tag attribute.
	 *
	 * @param name (string) attribute name
	 */
	this.removeAttribute = function(name) {
		delete this.attrs[name];
	}
	
	/** 
	 * Generates full tag HTML.
	 *
	 * @return string
	 */
	this.toString = function() {
		var html = '<' + this.name;
		for (var name in this.attrs) if (this.attrs[name]) {
			html += (' ' + name + '="' + this.attrs[name] + '"');
		}
		return (this.content)
			? (html + '>' + this.content + '</' + this.name + '>')
			: (html + ' />');
	}
}
